<html>
<head><meta charset="utf-8"><title>In a closure, how do I move some things but not others? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html">In a closure, how do I move some things but not others?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="213001248"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001248" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001248">(Oct 12 2020 at 06:17)</a>:</h4>
<p>If I prefix a closure with <code>move</code>, it'll move <em>everything</em> it references. Is there any way to make a closure move <em>one</em> thing and continue to handle the rest by reference?</p>



<a name="213001340"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001340" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001340">(Oct 12 2020 at 06:19)</a>:</h4>
<p>For instance, if I write:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">            </span><span class="k">loop</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">stream</span><span class="p">,</span><span class="w"> </span><span class="n">peer</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">listener</span><span class="p">.</span><span class="n">accept</span><span class="p">()</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="w">                </span><span class="n">s</span><span class="p">.</span><span class="n">spawn</span><span class="p">(</span><span class="o">|</span><span class="n">_</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
</code></pre></div>

<p>then rust will complain that <code>peer</code> might outlive that closure. Changing the closure to <code>move</code>, however, will cause it to move <em>other</em> things that it references, which need to be used repeatedly in iterations of the loop.</p>



<a name="213001346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001346">(Oct 12 2020 at 06:19)</a>:</h4>
<p>I'd like to tell the closure to take ownership of <code>peer</code>, without telling it to take ownership of anything <em>else</em>.</p>



<a name="213001350"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001350" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001350">(Oct 12 2020 at 06:19)</a>:</h4>
<p><code>let other_things = &amp;other_things;</code> just before the closure usually works for me</p>



<a name="213001354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001354">(Oct 12 2020 at 06:19)</a>:</h4>
<p>you can make all the references you need ahead of time.</p>



<a name="213001388"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001388" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001388">(Oct 12 2020 at 06:20)</a>:</h4>
<p>sniped</p>



<a name="213001403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001403">(Oct 12 2020 at 06:20)</a>:</h4>
<p>Unfortunately you have to do it for each reference at a time</p>



<a name="213001433"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001433" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001433">(Oct 12 2020 at 06:20)</a>:</h4>
<p>i wonder if a member of T-lang could get annoyed enough by this to get some motion towards an improvement in this area</p>



<a name="213001461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001461">(Oct 12 2020 at 06:21)</a>:</h4>
<p>Ah. I'd been thinking about how I express the <em>move</em> of one thing explicitly, and it hadn't occurred to me to use <code>move</code> and then <em>un</em>-<code>move</code> all the other things by making them references.</p>



<a name="213001475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001475">(Oct 12 2020 at 06:21)</a>:</h4>
<p><span class="user-mention silent" data-user-id="224471">Lokathor</span> <a href="#narrow/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F/near/213001433">said</a>:</p>
<blockquote>
<p>i wonder if a member of T-lang could get annoyed enough by this to get some motion towards an improvement in this area</p>
</blockquote>
<p>Open to suggestion. :)</p>



<a name="213001530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001530">(Oct 12 2020 at 06:22)</a>:</h4>
<p>initial "obvious" fix, <code>move(ident, ident..) || { ... }</code> will move <em>just</em> the idents listed</p>



<a name="213001539"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001539" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001539">(Oct 12 2020 at 06:22)</a>:</h4>
<p><span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span></p>



<a name="213001545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001545">(Oct 12 2020 at 06:22)</a>:</h4>
<p>That sounds pretty reasonable.</p>



<a name="213001566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001566">(Oct 12 2020 at 06:23)</a>:</h4>
<p>I wonder if one of the people who regularly discusses things with the lang team would be willing to propose this? :)</p>



<a name="213001581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001581">(Oct 12 2020 at 06:23)</a>:</h4>
<p>oh no XD</p>



<a name="213001584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001584">(Oct 12 2020 at 06:23)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F/near/213001350">said</a>:</p>
<blockquote>
<p><code>let other_things = &amp;other_things;</code> just before the closure usually works for me</p>
</blockquote>
<p>Sure enough, that works perfectly, thank you.</p>



<a name="213001633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001633">(Oct 12 2020 at 06:24)</a>:</h4>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> This is definitely one of those cases of "don't know the pattern yet".</p>



<a name="213001636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001636">(Oct 12 2020 at 06:24)</a>:</h4>
<p>You're welcome!</p>



<a name="213001645"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001645" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001645">(Oct 12 2020 at 06:24)</a>:</h4>
<p>Yeah this could be documented better</p>



<a name="213001647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001647">(Oct 12 2020 at 06:24)</a>:</h4>
<p>I'm currently 1) using crossbeam::scope in production for the first time, and 2) attempting to use flume in production for the first time.</p>



<a name="213001665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001665">(Oct 12 2020 at 06:24)</a>:</h4>
<p>This and splitting structs so the closure doesn't capture all of them fit in with my 'discoverability' blog post really well</p>



<a name="213001679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001679">(Oct 12 2020 at 06:25)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F/near/213001665">said</a>:</p>
<blockquote>
<p>This and splitting structs so the closure doesn't capture all of them fit in with my 'discoverability' blog post really well</p>
</blockquote>
<p>I'm incredibly excited about RFC 2229 on that front.</p>



<a name="213001687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001687">(Oct 12 2020 at 06:25)</a>:</h4>
<p>Maybe I should make a 'rust tips and tricks' post <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="213001695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001695">(Oct 12 2020 at 06:25)</a>:</h4>
<p>TWiR is calling you.</p>



<a name="213001736"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001736" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001736">(Oct 12 2020 at 06:26)</a>:</h4>
<p>I'm looking to have self-contained threads that act as "server thread factories": send them a message with server configuration details, they make a new thread listening on a port and send you the port number.</p>



<a name="213001760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001760">(Oct 12 2020 at 06:26)</a>:</h4>
<p>(And the "server thread factory" thread, in addition to handling those messages, caches things shared between the server threads.)</p>



<a name="213001765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001765">(Oct 12 2020 at 06:26)</a>:</h4>
<p>TWIR has a tendency to ignore prs IME :/</p>



<a name="213001802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213001802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213001802">(Oct 12 2020 at 06:27)</a>:</h4>
<p><a href="https://github.com/rust-lang/this-week-in-rust/pull/1434">https://github.com/rust-lang/this-week-in-rust/pull/1434</a></p>



<a name="213002127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213002127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213002127">(Oct 12 2020 at 06:33)</a>:</h4>
<p>Interesting bit of history: RFC 2229 may help solve the <em>very first</em> borrow checker issue I ever ran into when I was learning Rust.</p>



<a name="213002187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213002187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213002187">(Oct 12 2020 at 06:34)</a>:</h4>
<p>As a test program, I tried to use the git2 crate and a fuse crate to build the obvious "mount a git tree as a directory" filesystem.</p>



<a name="213002206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213002206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213002206">(Oct 12 2020 at 06:34)</a>:</h4>
<p>Which worked perfectly, until I tried to put the git2 tree and the fuse connection into the same struct.</p>



<a name="213002230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213002230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213002230">(Oct 12 2020 at 06:35)</a>:</h4>
<p>At which point I ran into lifetime issues trying to write methods, because the methods borrowed all of <code>&amp;self</code>.</p>



<a name="213002395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213002395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213002395">(Oct 12 2020 at 06:38)</a>:</h4>
<p>my biggest initial issues with rust were matching and auto-ref / auto-deref related. coming from Haskell at the time, which essentially doesn't have reference types at all similar to how rust has them, i was always lost with what types my numbers were.</p>



<a name="213002753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213002753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213002753">(Oct 12 2020 at 06:47)</a>:</h4>
<p>I don't actually remember the first errors I ran into</p>



<a name="213002759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213002759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213002759">(Oct 12 2020 at 06:47)</a>:</h4>
<p>I remember being really confused by <code>if let</code></p>



<a name="213002773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213002773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213002773">(Oct 12 2020 at 06:47)</a>:</h4>
<p>And the module system, I didn't realize <code>mod</code> and <code>use</code> did different things</p>



<a name="213013224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213013224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213013224">(Oct 12 2020 at 08:59)</a>:</h4>
<p>obligatory plug of Niko's post about this <a href="https://smallcultfollowing.com/babysteps/blog/2018/04/24/rust-pattern-precise-closure-capture-clauses/">https://smallcultfollowing.com/babysteps/blog/2018/04/24/rust-pattern-precise-closure-capture-clauses/</a></p>



<a name="213051586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213051586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213051586">(Oct 12 2020 at 15:17)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="125270">@scottmcm</span>  for the <code>move(ident)</code> capture idea above -- another reason why the postfix <code>expr.move</code> idea is better than <code>move(expr)</code> for avoiding ambiguity.</p>



<a name="213066191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213066191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213066191">(Oct 12 2020 at 17:33)</a>:</h4>
<p>Interesting.  Definitely reminding me of C++'s <code>[=foo, &amp;bar]</code>.</p>



<a name="213066379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213066379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213066379">(Oct 12 2020 at 17:35)</a>:</h4>
<p>I think there's another way to possibly take this -- to say that the <a href="#narrow/stream/213817-t-lang/topic/Spitballing.3A.20a.20.60move(x).60.20expression.3F"><code>place.move</code> conversation</a> could be a way to address Josh's "how I express the move of one thing explicitly" request: rather than <code>move ||</code>ing the whole closure, you could opt-in to place-wise <code>move</code> capture by using <code>.move</code> inside the closure.</p>



<a name="213066455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213066455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213066455">(Oct 12 2020 at 17:36)</a>:</h4>
<p>(I haven't thought through the implications of that -- probably can't work for things moved into the closure but then used by-ref inside the closure)</p>



<a name="213066635"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213066635" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213066635">(Oct 12 2020 at 17:38)</a>:</h4>
<p><del>Oh, yeah, Josh's example in this thread needs it in a loop, so <code>listener.move.accept()?;</code> wouldn't be ok.  And something like</del></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">listener</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">listener</span><span class="p">.</span><span class="k">move</span><span class="p">;</span><span class="w"></span>
<span class="k">loop</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">stream</span><span class="p">,</span><span class="w"> </span><span class="n">peer</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">listener</span><span class="p">.</span><span class="n">accept</span><span class="p">()</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
</code></pre></div>

<p><del>doesn't seem much better than the existing options of local shadows for some bindings.</del></p>
<p>EDIT: Uh, no, that's all outside the closure.  Unclear then.</p>



<a name="213067081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213067081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213067081">(Oct 12 2020 at 17:43)</a>:</h4>
<p><code>.move</code> could force a closure to be just <code>FnOnce</code> though, in situations where the captures would otherwise allow reuse</p>



<a name="213067195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213067195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213067195">(Oct 12 2020 at 17:44)</a>:</h4>
<p>I just want to point out that <code>.move</code> has gained a lot of meanings in this thread. I would hate to teach this</p>



<a name="213067292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213067292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213067292">(Oct 12 2020 at 17:45)</a>:</h4>
<p>additionally, the use of <code>.move</code> to move a variable from inside a closure seems like a really non-local mechanism</p>



<a name="213067809"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213067809" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213067809">(Oct 12 2020 at 17:50)</a>:</h4>
<p>(er, I actually meant <a href="#narrow/stream/213817-t-lang/topic/Spitballing.3A.20a.20.60move(x).60.20expression.3F">this thread</a>, which is apparently continued here)</p>



<a name="213068016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213068016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213068016">(Oct 12 2020 at 17:52)</a>:</h4>
<p>They seem consistent to me, even if subtle</p>



<a name="213068105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213068105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213068105">(Oct 12 2020 at 17:53)</a>:</h4>
<p>'consistent' and 'lots of meanings' are not mutually exclusive</p>



<a name="213068369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213068369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213068369">(Oct 12 2020 at 17:56)</a>:</h4>
<p>Yeah, what closures do with idents they mention is already extremely non-local.  You can't look at <code>|| x.foo()</code> and know whether it's <code>Fn</code>, <code>FnMut</code>, or <code>FnOnce</code>.  If anything, I think <code>|| x.move.foo()</code> would be far clearer than the existing cases, since moving like that makes it locally obvious that it <em>has</em> to be <code>FnOnce</code>.</p>



<a name="213096246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213096246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213096246">(Oct 12 2020 at 23:32)</a>:</h4>
<p>if an identifier is used by value in the closure, value referenced by it will get moved even without the <code>move</code> keyword.</p>



<a name="213096273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213096273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213096273">(Oct 12 2020 at 23:33)</a>:</h4>
<p>so <code>let thing = identity(thing);</code> can be a good way to force a move of the specific <code>thing</code>.</p>



<a name="213097936"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213097936" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213097936">(Oct 13 2020 at 00:02)</a>:</h4>
<p>Yeah, as discussed in the lang thread, most of the things this could do work with <code>{x}</code> instead of <code>x.move</code>.</p>



<a name="213121171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/In%20a%20closure%2C%20how%20do%20I%20move%20some%20things%20but%20not%20others%3F/near/213121171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/In.20a.20closure.2C.20how.20do.20I.20move.20some.20things.20but.20not.20others.3F.html#213121171">(Oct 13 2020 at 07:51)</a>:</h4>
<p>But <code>let x = x.move;</code> and identity / <code>{}</code> variants lead to an FnOnce closure (or if the thing is <code>Copy</code>, it won't suffice to make it capture it by move). Sometimes you want to <code>move</code> for lifetime reasons,.while still wanting an <code>Fn{,Mut}</code>.<br>
Although for the non-<code>Copy</code> case, maybe wrapping that in an <code>if false</code> can do the trick <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>